package com.thalesgroup.nl.trtdelft.dein.mapper.example;

import com.thalesgroup.nl.trtdelft.dein.mapper.AbstractMappedWebService;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author AbdelhaqA
 */
public class TripPlan extends AbstractMappedWebService {

    private String listOfHotels = null;
    private String weatherForecast = null;

    @Override
    public void start() {
        super.start();

        //copying the querying receive values to query send
        String addressCode = (String) queryReceiveVariableContent.get("addressCode");
        String genreCode = (String) queryReceiveVariableContent.get("genreCode");

        HashMap<String, Serializable> qSendContent = new HashMap<>();
        qSendContent.put("addressCode", addressCode);
        qSendContent.put("genreCode", genreCode);

        List<String> capabilities = getRequiredCababilityForQuerySendVariables();

        for (String capability : capabilities) {

            Map<String, Serializable> map = getQuerySendServiceParameterElements(capability);

            if (map.containsKey("addressCode") && map.containsKey("genreCode")) {
                querySendVariableContent.put(capability, qSendContent);
                sendMapperInformationInterface.sendQuery(capability);
            }
        }
    }
    @Override
    @SuppressWarnings("unchecked")
    public void input(String senderAddress, String inputVariableName, Serializable input) {

        System.out.println("senderAddress=" + senderAddress + "inputVariableName=" + inputVariableName + " input=" + input);

        if (inputVariableName.equals("listOfHotels") ) {
            try {
                listOfHotels = (String) input;

                //extract the coordinates
                DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(listOfHotels));

                Document doc = db.parse(is);
                String coord = doc.getElementsByTagName("Coordinates").item(0).getTextContent();
                //NodeList coord = doc.getElementsByTagName("Coordinates");

                HashMap<String, Serializable> qSendContent = new HashMap<>();
                qSendContent.put("gpsWeather", coord);
                
                List<String> capabilities = getRequiredCababilityForQuerySendVariables();

                for (String capability : capabilities) {
                    if (capability.equals("YahooWeatherService")) {
                        querySendVariableContent.put(capability, qSendContent);
                        sendMapperInformationInterface.sendQuery(capability);
                    }
                }                

            } catch (SAXException ex) {
                Logger.getLogger(TripPlan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(TripPlan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(TripPlan.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (inputVariableName.equals("weatherForecast")) {
            // send list of Hotels and weather to the interface
            weatherForecast = (String) input;            
            outputs.put("weatherForecast", weatherForecast);
            outputs.put("listOfHotels", listOfHotels);           
          //  sendMapperInformationInterface.sendOutput();
        }
    }

    @Override
    public void computeOutput() throws IllegalStateException {
    }

    @Override
    public void computeOutput(String variableName) throws IllegalStateException {
        computeOutput();
    }

    @Override
    public Serializable getCurrentOutput(String variableName) throws IllegalStateException {
        return outputs.get(variableName);
    }

}
